define(['config', 'text!app/module/system/lossRelation.html', 'laypage',
    'mydialog', 'autocomplete', 'jqGrid', 'common', 'laydate'
], function (config, viewHTML, laypage,
    mydialog, autocomplete, jqGrid, common, laydate) {
	
	var token = (cache.go("token") === 'undefined') ? 'none' : cache.go("token");

    var grid_url = "/api/loss";

    var model = avalon.define({
    	$id : "lossRelation",
    	lossRelationInfo: {
                id: "",
                name: "",
                status: "",
                describes: "",
                remarks: "",
                businesslistId: "",
                createUser: "admin"
        },
         // 查询条件
         query: {
             name: ""
         },

         statusArr: [],
         selected: [],
         roles: [],
         
         init: function () {
             model.loadGrid();
             model.addEvent();
         },
         loadGrid: function () {
             function t(val, opt, row) {
                 var html_con = '<div class="operating" data-id="' + row.id + '"><span class="fa fa-edit mrb" title="修改"><a href="javascript:void();" /></span><span class="fa fa-trash-o mrb" title="删除"></span>' + (row.is_deleted == 1 ? '<span class="fa fa-reply mrb" title="恢复"></span>' : '') + '</div>';
                 return html_con;
             }


             var i = Public.setGrid();
             $("#grid").jqGrid({
                 loadBeforeSend: function (jqXHR) {
                     jqXHR.setRequestHeader("X-AuthToken", token);
                 },
                 datatype: "json",
                 mtype: "POST",
                 url: grid_url+"/list",
                 postData: model.query.$model,
                 loadComplete: function (data) {},
                 loadError: function (xhr, status, error) {
                     alert(JSON.parse(xhr.responseText).msg);
                 },
                 colModel: [{
                         name: "operating",
                         label: "操作",
                         width: '5%',
                         formatter: t
                     },
                     {
                         name: "id",
                         label: "id",
                         width: '10%',
                         hidden: true
                     },
                     {
                         name: "name",
                         label: "功能区名称",
                         width: '10%'
                     },
                     {
                         name: "status",
                         label: "是否模板",
                         width: '10%',
                         hidden:true
                     },
                     {
                         name: "describes",
                         label: "描述",
                         width: '13%',
                     },
                     {
                         name: "remarks",
                         label: "备注",
                         width: "7%"
                     },
                     {
                         name: "createTime",
                         label: "创建日期",
                         width: '10%',
                         formatter: function (value) {
                          return  Public.custDatefmter(value);
                         }
                     }
                 ],
                 cmTemplate: {
                     sortable: false,
                     title: false,
                     align: "center"
                 },
                 // 分页
                 page: 1,
                 rowNum: 10,
                 rowList: [10, 20, 30],
                 height: i.h,
                 autowidth: true,
                 pager: '#page',
                 sortname: 'id',
                 viewrecords: true,
                 forceFit: false,
                 rownumbers: true,
                 multiselect: true,
                 multiboxonly: true,
                 sortorder: "desc",
                 jsonReader: {
                     root: "data.list",
                     records: "data.totalRow",
                     total: "data.totalPage",
                     repeatitems: false,
                     id: "id"
                 }
             })
         },
         reset: function () {
             model.query = {
                 name: ""
             };
             model.queryData();
         },
         queryData: function () {
             $("#grid").jqGrid("setGridParam", {
                 url: grid_url+"/list",
                 datatype: "json",
                 mtype: 'POST',
                 page:1,
                 postData: model.query.$model
             }).trigger("reloadGrid");
         },
         del: function (ids) {
             dialog({
                 title: "确认操作",
                 content: "确定删除？ 此操作不可恢复！",
                 okValue: "确定",
                 ok: function () {

                     $.ajax({
                         type: "POST",
                         url: "/api/loss/delete",
                         data: {
                             "ids": ids
                         },
                         beforeSend: function (xhr) {
                             xhr.setRequestHeader("X-AuthToken", token);
                         },
                         success: function (data, status, xhr) {
                             if (data) {
                                 $('#grid').jqGrid('delRowData', ids);
                             }
                             model.queryData();
                         },
                         error: function (responseData, textStatus, errorThrown) {
                             alert("Failed: " + responseData);
                         }

                     });

                 },
                 cancelValue: "取消",
                 cancel: function () {}
             }).showModal();
         },
         addEvent: function () {
        	 /** 打开新的table 跳转页面 */
        	 $(".fr").on("click", "#add", function (e) {
        		 top.topManager.openPage({
        			id:"addLossRelastion",
        			title:"新增功能区域",
        			href:'index.html#!/125?bomNo=',
        			reload:true
        		 })
        	 });
        	 
        	 
        	 
             $(".grid-wrap").on("click", ".fa-edit", function (e) {
            	  var t = $(this).parent().data("id");
                  var rowData = $("#grid").jqGrid('getRowData',t);
                  var bomNo = rowData.bomNo;
            	 top.topManager.openPage({
         			id:"addLossRelastion",
         			title:"新增功能区域",
         			href:'index.html#!/125?bomNo='+bomNo,
         			reload:true
         		 })
             });
             // 删除
             $(".grid-wrap").on("click", ".fa-trash-o", function (t) {
                 var e = $(this).parent().data("id");
                 model.del(e);
             });
             $("#search").click(function (e) {
                 e.preventDefault();
                 model.queryData();
             });
             $(".fa-refresh").click(function (e) {
                 e.preventDefault();
                 model.reset();
                 model.queryData();
             });
             $("#btn-batchDel").click(function (e) {
                 e.preventDefault();
                 // if (Business.verifyRight("BU_DELETE")) {
                 var t = $("#grid").jqGrid("getGridParam", "selarrrow");
                 t.length ? model.del(t.join()) : parent.Public.tips({
                     type: 2,
                     content: "请选择需要删除的项"
                 })
                 // }
             });
         },
         initlossRelation: function () {
             model.lossRelationInfo = {
                     id: "",
                     name: "",
                     status: "",
                     describes: "",
                     remarks: "",
                     businesslistId: "",
                     createUser: "admin"
                 }
             }
    });
    
    // 定义视图模块
    var viewmod = {
        viewtSrc: viewHTML,
        viewInit: function () {
            model.init();
        }, // 模块view加载完成后的回调方法，在刷新（F5）时可能调用两次
        viewDestroy: function () {} // 切换菜单时销毁原来的view
    };
    return viewmod;
	
	
});
